﻿/******************************************************************************
 * 【本类功能概述】                                 					      *
 *  版权所有（C）2017-                                           *
 *  保留所有权利。                                                            *
 ******************************************************************************
 *  作者 : <hughjk>
 *  创建时间:2019年5月21日 18:35:25
 *  文件描述:读写文件
 *****************************************************************************/

namespace Util
{
    using System.Collections.Generic;
    using System.Text;
    using System.Text.RegularExpressions;

    public class ConfigFileSave
    {
        public string Name;//Data.config
        public string Path;//D:/AA/
        public Dictionary<string, Dictionary<string, string>> Data = new Dictionary<string, Dictionary<string, string>>();
        public StringBuilder Sb = new StringBuilder();
        static Regex reg = new Regex(@"\[(.+?)]");

        public void Set(string ComName, string Key, string V)
        {
            Data.GetOrAdd(ComName, new Dictionary<string, string>()).SetOrAdd(Key, V);
        }
        public string Get(string ComName, string Key)
        {
            return Data.GetOrAdd(ComName, new Dictionary<string, string>()).GetOrAdd(Key);
        }
        static Dictionary<string, ConfigFileSave> cache = new Dictionary<string, ConfigFileSave>();

        public static ConfigFileSave Load(string path, string name)
        {
            ConfigFileSave Config = cache.GetOrAdd(path + name);
            if (Config.isNull())
            {
                Config = new ConfigFileSave();
                Config.Path = path;
                Config.Name = name;

                var Filelin = FileIO.LoadLines(path, name, Encoding.Default);
                string CurComName = "";
                for (int i = 0; i < Filelin.Length; i++)
                {
                    var lins = Filelin[i];
                    if (lins.Length > 0)
                    {
                        var mc = reg.Matches(lins);
                        if (lins[0] == '[' && mc.Count >= 1)
                        {
                            var m = mc[0];
                            var s = m.Groups[1];
                            CurComName = s.ToString();
                        }
                        else
                        {
                            var kv = lins.toSplit('=');
                            if (kv.Length >= 2)
                            {
                                Config.Set(CurComName, kv[0], kv[1]);
                            }
                        }
                    }
                }
            }
            return Config;
        }
        public void Save()
        {
            Sb.Clear();
            var eCom = Data.GetEnumerator();
            while (eCom.MoveNext())
            {
                var ComName = eCom.Current.Key;
                Sb.Append('[');
                Sb.Append(ComName);
                Sb.Append("]\n");
                var ve = eCom.Current.Value.GetEnumerator();
                while (ve.MoveNext())
                {
                    Sb.Append(ve.Current.Key);
                    Sb.Append('=');
                    Sb.Append(ve.Current.Value);
                    Sb.Append('\n');
                }
                Sb.Append('\n');
            }
            FileIO.Save(Path, Name, Sb.ToString(), Encoding.Unicode);
        }
    }
}